
class Node {
    constructor(name) {
        this.name = name
        this.id = name
        this.nodeStatus = -1  // -1没开始 0 失败 1 成功 2 进行中
    }

    async tick() {

    }
}

class SequenceNode extends Node {
    constructor(name, nodeList) {
        super(name);
        this.nodeList = nodeList
    }

    async tick() {
        var hasRunning = false
        for (let n = 0; n < this.nodeList.length; n++) {
            const node = this.nodeList[n];
            var nodeStatus = await node.tick()
            if (nodeStatus == 1) {

            }
            else if (nodeStatus == 0) {
                return nodeStatus
            }
            else if (nodeStatus == 2) {
                hasRunning = true
            }
        }
        if (hasRunning) {
            return 2
        }
        else {
            return 1
        }
    }
}


class ChooseNode extends SequenceNode {
    constructor(name, nodeList) {
        super(name, nodeList)
    }

    async tick() {
        for (let n = 0; n < this.nodeList.length; n++) {
            const node = this.nodeList[n];
            var nodeStatus = await node.tick()
            if (nodeStatus == 1) {
                return nodeStatus
            }
            else if (nodeStatus == 0) {

            }
            else if (nodeStatus == 2) {
                return nodeStatus
            }
        }
    }
}

class ParallelNode extends SequenceNode {
    constructor(name, nodeList) {
        super(name, nodeList)
    }
    async tick() {
        const node = this.nodeLisnodet[n];
        var nodeStatus = await node.tick()
        if (nodeStatus == 1) {
            return nodeStatus
        }
        else if (nodeStatus == 0) {

        }
        else if (nodeStatus == 2) {
            return nodeStatus
        }
    }
}


class InversionDecroator extends Node {
    constructor(name, node) {
        super(name)
        this.node = node
        this.nodeStatus = 2  // 0 失败 1 成功 2 进行中
    }
    async tick() {
        var result = await this.node.tick()
        if (result == 1) {
            return 0;
        } else if (result == 0) {
            return 1
        }
        else if (result == 2) {
            return 2
        }
    }
}


class ForDecroator extends InversionDecroator {
    constructor(name, node, times) {
        super(name, node)
        this.times = times;
    }
    async tick() {
        var hasError = false;
        for (let i = 0; i < this.times; i++) {
            var result = await this.node.tick()
            if (result == 0) {
                hasError = true
            }
        }
        if (hasError) {
            return 0
        } else {
            return 1
        }
    }
}


export { Node, SequenceNode, ChooseNode, ParallelNode, InversionDecroator, ForDecroator }